//
<script setup lang="ts">
import { phoneLogin } from '@/services/login'
import { useMemberStore } from '@/stores'
import type { LoginResult } from '@/types/member'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
//loginUser为登录用户对象，用来保存登录成功后的用户信息
const loginUser = ref<LoginResult>()
const mem = useMemberStore() //持久化数据模型
// 获取用户头像

//获取调用wx.login()微信登录code临时登录凭证，用于后端获取openId开发者id
let loginCode = ''
onLoad(async () => {
  const res = await wx.login()
  loginCode = res.code
})
//授权手机号一键登录
const getUserPhone: UniHelper.ButtonOnGetphonenumber = async (e) => {
  const res = await phoneLogin(loginCode, e.detail.code)

  //得到从后端响应过来的WxUser微信用户
  loginUser.value = res.result
  //保存用户信息答pinia中
  mem.setProfile(loginUser.value)
  //登录成功提示
  uni.showToast({ icon: 'success', title: '登录成功' })
  setTimeout(() => {
    //使用uni.switchTab跳转到底部tabbar中的我的
    uni.switchTab({ url: '/pages/my/my' })
  }, 2000)
}
</script>
<template>
  <view class="viewport">
    <view class="logo">
      <image src="@\static\images\ketailogo.png" />
    </view>
    <view class="login">
      <button class="button phone" open-type="getPhoneNumber" @getphonenumber="getUserPhone">
        <text class="icon icon-phone"></text>
        手机号一键登录
      </button>
      <view class="extra">
        <view class="caption">
          <text>其他登录方式</text>
        </view>
        <view class="options">
          <!-- 通用模拟登录 -->
          <button>
            <text class="icon icon-phone">模拟快捷登录</text>
          </button>
        </view>
      </view>
      <view class="tips">登录/注册即视为你同意《服务条款》和《科泰购隐私协议》</view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 20rpx 40rpx;
}

.logo {
  flex: 1;
  text-align: center;
  image {
    width: 240rpx;
    height: 220rpx;
    margin-top: 15vh;
  }
}

.login {
  display: flex;
  flex-direction: column;
  height: 60vh;
  padding: 40rpx 20rpx 20rpx;

  .input {
    width: 100%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    border: 1px solid #ddd;
    padding-left: 30rpx;
    margin-bottom: 20rpx;
  }

  .button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    color: #fff;
    .icon {
      font-size: 40rpx;
      margin-right: 6rpx;
    }
  }

  .phone {
    background-color: #1354a2;
  }

  .wechat {
    background-color: #1354a2;
  }

  .extra {
    flex: 1;
    padding: 70rpx 70rpx 0;
    .caption {
      width: 440rpx;
      line-height: 1;
      border-top: 1rpx solid #ddd;
      font-size: 26rpx;
      color: #999;
      position: relative;
      text {
        transform: translate(-40%);
        background-color: #fff;
        position: absolute;
        top: -12rpx;
        left: 50%;
      }
    }

    .options {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 70rpx;
      button {
        padding: 0;
        background-color: transparent;
      }
    }

    .icon {
      font-size: 24rpx;
      color: #444;
      display: flex;
      flex-direction: column;
      align-items: center;

      &::before {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 80rpx;
        height: 80rpx;
        margin-bottom: 6rpx;
        font-size: 40rpx;
        border: 1rpx solid #444;
        border-radius: 50%;
      }
    }
    .icon-weixin::before {
      border-color: #1354a2;
      color: #1354a2;
    }
  }
}

.tips {
  position: absolute;
  bottom: 80rpx;
  left: 20rpx;
  right: 20rpx;
  font-size: 22rpx;
  color: #999;
  text-align: center;
}
</style>
